package com.example.libraryapp.fragment.bookrack;

import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.libraryapp.Constant;
import com.example.libraryapp.R;
import com.example.libraryapp.activity.bookDetail.BookDetailActivity;
import com.example.libraryapp.activity.bookList.BookListActivity;
import com.example.libraryapp.activity.login.LoginActivity;
import com.example.libraryapp.adapter.BookAdapter;
import com.example.libraryapp.adapter.BookBorrowAdapter;
import com.example.libraryapp.adapter.base.BaseAdapter;
import com.example.libraryapp.bean.Book;
import com.example.libraryapp.bean.BookBorrow;
import com.example.libraryapp.bean.User;
import com.example.libraryapp.bean.response.BaseResponse;
import com.example.libraryapp.network.base.BaseFragment;
import com.example.libraryapp.network.base.BasePresenter;
import com.example.libraryapp.util.LoginUtil;
import com.example.libraryapp.widget.CnToolbar;

import java.util.List;

/**
 * @author 伍凯铭
 * @since 2020/6/18
 */
public class BookrackFragment extends BaseFragment<BookrackContract.View,BookrackPresenterIml>
        implements BookrackContract.View, BaseAdapter.OnItemClickListener{

    private TextView mNotLoginText;

    private RecyclerView mBookBorrowView;

    private BookBorrowAdapter mBookBorrowAdapter;

    private User mUser;

    @Override
    public void onStart() {
        super.onStart();
        checkLogin();
        if(mUser != null) {
            showLoading();
            mPresenter.getAllBookBorrow(mUser.getUserId());
        }
    }

    private void checkLogin() {
        mUser = LoginUtil.getNowUser(getContext());
        if(mUser != null) {
            mBookBorrowView.setVisibility(View.VISIBLE);
            mNotLoginText.setVisibility(View.GONE);
        }else {
            mBookBorrowView.setVisibility(View.GONE);
            mNotLoginText.setVisibility(View.VISIBLE);
        }
    }

    /**
     *
     * @param hidden True代表隐藏，False代表看得见
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden) {
            checkLogin();
        }
    }

    @Override
    protected void initView(View view) {
        mNotLoginText = view.findViewById(R.id.not_login_text);
        initNotLoginText();

        mBookBorrowView = view.findViewById(R.id.recycler_view);
        mBookBorrowAdapter = new BookBorrowAdapter(getContext());
        mBookBorrowAdapter.setOnItemClickListener(this);
        mBookBorrowView.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false));
        mBookBorrowView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        mBookBorrowView.setAdapter(mBookBorrowAdapter);
    }

    private void initNotLoginText() {
        SpannableString spannableString = new SpannableString("您还没有登录,请先登录/注册");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(getActivity(),R.color.royalblue));
        LoginClickableSpan clickableSpan = new LoginClickableSpan();
        spannableString.setSpan(clickableSpan, spannableString.length()-5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan, spannableString.length()-5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mNotLoginText.setMovementMethod(LinkMovementMethod.getInstance());
        mNotLoginText.setText(spannableString);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bookrack;
    }

    @Override
    protected BookrackPresenterIml initPresenter() {
        return new BookrackPresenterIml();
    }

    @Override
    protected void initToolBar(CnToolbar toolbar) {
        toolbar.setTitle(R.string.bookrack);
    }

    @Override
    public void onItemClick(View view, int position) {
        Book book = mBookBorrowAdapter.getItem(position).getBook();
        Intent intent = new Intent(getActivity(), BookDetailActivity.class);
        intent.putExtra(Constant.BOOK_ID,book.getBookId());
        startActivity(intent);
    }

    @Override
    public void showAllBookBorrow(BaseResponse<List<BookBorrow>> response) {
        mBookBorrowAdapter.refreshData(response.getResult());
    }

    class LoginClickableSpan extends ClickableSpan {

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(@NonNull View widget) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }
    }
}
